package com.verisette.ncos.web.master.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import org.corp.agi.common.annotation.RequiresAuthentication;
import org.corp.agi.common.bean.LabelValueBean;
import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.service.bean.ServiceRequest;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.util.ServiceRequestFactory;
import com.verisette.ncos.common.PrivConst;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterUserGroup;
import com.verisette.ncos.service.master.gateway.RoleManagementService;

/**
 * 
 * {Description here}
 *
 * @author Ratapong Wongpiang (Earth)
 * @since Jul 1, 2010 11:56:32 AM
 * @see
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class RoleManagementAction extends BaseAction {

	private Logger logger = Logger.getLogger(RoleManagementAction.class);
	
	private MasterUserGroup userGroupBean;
	private MasterUserGroup searchUserGroupBean;
	
	private String selectedId;
	private String clickSearchFlag;
	private String editMode;
	
	private List<MasterUserGroup> resultList;
	private List<LabelValueBean> statusList;
	
	private PagingBean pagingBean;
//	private int requestPage;
//	private int rowsPerPage;
	
	//for sort data
	private String sortMode;
	
	private RoleManagementService roleManagementService;
	@Autowired
	@Required
	public void setMasterRoleManagementService(RoleManagementService roleManagementService) {
		this.roleManagementService = roleManagementService;
	}
	
	@RequiresAuthentication(granted={PrivConst.S070100, PrivConst.S070101})
	public String init(){
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": init" );
		
		pagingBean = new PagingBean(ProjectConstant.ROW_PER_PAGE);
//		pagingBean.setRowsPerPage(ROW_PER_PAGE);
//		requestPage = 1;
//		rowsPerPage = ROW_PER_PAGE;
		
		return "searchRole";
		
	}

	@RequiresAuthentication(granted={PrivConst.S070101})
	public String search() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": searchRole" );
		
		if(clickSearchFlag.equals("true")){
			ServiceRequest<MasterUserGroup> inquiryUserGroupReq = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
			inquiryUserGroupReq.setValueObject(searchUserGroupBean);
			
			//add by pimonrath (kae) 08/11/2010
			if(logger.isDebugEnabled()){
				logger.debug("sortMode-->"+this.sortMode);
				
			}
			//****check sort mode
			if(StringUtils.isEmpty(this.sortMode)){
				this.sortMode = PagingBean.ORDER_ASC;
			}
			
			if(logger.isDebugEnabled()){
				logger.debug("sortMode-->"+this.sortMode);
			}
			
			pagingBean.setOrderMode(this.sortMode);
			
			//pagingBean.setCurrentPage(requestPage);
			//pagingBean.setRowsPerPage(rowsPerPage);
			inquiryUserGroupReq.setPaging(pagingBean);
			
			ServiceResponse<List<MasterUserGroup>> inquiryUserGroupRes = roleManagementService.inquiryUserGroup(inquiryUserGroupReq);
			
			if(isServiceResponseSuccess(inquiryUserGroupRes)){
				resultList = inquiryUserGroupRes.getValueObject();
				
				// set usgStatus for show
				for(MasterUserGroup userGroup : resultList){
					if(ProjectConstant.USER_PROFILE_STATUS_ACTIVE.equals(userGroup.getUsgStatus())){
						userGroup.setUsgStatus("Active");
						
					}else{
						userGroup.setUsgStatus("Inactive");
						
					}
					
				}
				
			}else{
				return "searchRole";
				
			}
			
			return "searchRole";
			
		}
		
		return "searchRole";
		
	}
	
	@RequiresAuthentication(granted={PrivConst.S070200})
	public String preAdd() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": initAddEditRole" );
		
		userGroupBean = new MasterUserGroup();
		userGroupBean.setUsgStatus(ProjectConstant.USER_PROFILE_STATUS_ACTIVE);
		
		initList();
		
		return "addEditRole";
		
	}
	
	@RequiresAuthentication(granted={PrivConst.S070201})
	public String add() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": addRole" );
		
		ServiceRequest<MasterUserGroup> createRoleReq = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		createRoleReq.setValueObject(userGroupBean);
		
		ServiceResponse createRoleRes = roleManagementService.createUserGroup(createRoleReq);
		
		if(isServiceResponseSuccess(createRoleRes)){
		
		}else{
			initList();
			
			return "addEditRole";
			
		}
		
		return search();
		
	}
	
	@RequiresAuthentication(granted={PrivConst.S070200})
	public String view() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": view" );
		
		ServiceRequest<MasterUserGroup> viewRoleReq = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		
		MasterUserGroup userGroup = new MasterUserGroup();
		userGroup.setUsgId(Integer.parseInt(selectedId));
		
		viewRoleReq.setValueObject(userGroup);
		
		ServiceResponse<MasterUserGroup> viewRoleRes = roleManagementService.viewUserGroup(viewRoleReq);
		
		if(isServiceResponseSuccess(viewRoleRes)){
			userGroupBean = viewRoleRes.getValueObject();
			
		}else{
			return "searchRole";
			
		}
		
		initList();
		
		return "viewRole";
	}
	
	@RequiresAuthentication(granted={PrivConst.S070200})
	public String preEdit() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": preEdit" );
		
		userGroupBean = new MasterUserGroup();
		userGroupBean.setUsgId(Integer.parseInt(selectedId));
		
		ServiceRequest<MasterUserGroup> viewRoleReq = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		viewRoleReq.setValueObject(userGroupBean);
		
		ServiceResponse<MasterUserGroup> viewUserGroupRes = roleManagementService.viewUserGroup(viewRoleReq);
		
		if(isServiceResponseSuccess(viewUserGroupRes)){
			userGroupBean = viewUserGroupRes.getValueObject();
			
			initList();
			editMode = "true";
			
		}else{
			return "searchRole";
			
		}
		
		return "addEditRole";
		
	}
	
	@RequiresAuthentication(granted={PrivConst.S070201})
	public String edit() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": edit" );
		
		ServiceRequest<MasterUserGroup> editRoleReq = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		editRoleReq.setValueObject(userGroupBean);
		
		ServiceResponse editRoleRes = roleManagementService.editUserGroup(editRoleReq);
		
		if(isServiceResponseSuccess(editRoleRes)){
			
		}else{
			initList();
			
			return "addEditRole";
			
		}
		
		return search();
		
	}
	
	private void initList(){
		statusList = new ArrayList<LabelValueBean>();
		statusList.add(new LabelValueBean("Active", "A"));
		statusList.add(new LabelValueBean("Inactive", "I"));
		
	}
	
	public List<MasterUserGroup> getResultList() {
		return resultList;
	}

	public void setResultList(List<MasterUserGroup> resultList) {
		this.resultList = resultList;
	}

	public PagingBean getPagingBean() {
		return pagingBean;
	}

	public void setPagingBean(PagingBean pagingBean) {
		this.pagingBean = pagingBean;
	}

//	public int getRequestPage() {
//		return requestPage;
//	}
//
//	public void setRequestPage(int requestPage) {
//		this.requestPage = requestPage;
//	}
//
//	public int getRowsPerPage() {
//		return rowsPerPage;
//	}
//
//	public void setRowsPerPage(int rowsPerPage) {
//		this.rowsPerPage = rowsPerPage;
//	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String getClickSearchFlag() {
		return clickSearchFlag;
	}

	public void setClickSearchFlag(String clickSearchFlag) {
		this.clickSearchFlag = clickSearchFlag;
	}

	public MasterUserGroup getUserGroupBean() {
		return userGroupBean;
	}

	public void setUserGroupBean(MasterUserGroup userGroupBean) {
		this.userGroupBean = userGroupBean;
	}

	public List<LabelValueBean> getStatusList() {
		return statusList;
	}

	public void setStatusList(List<LabelValueBean> statusList) {
		this.statusList = statusList;
	}

	public MasterUserGroup getSearchUserGroupBean() {
		return searchUserGroupBean;
	}

	public void setSearchUserGroupBean(MasterUserGroup searchUserGroupBean) {
		this.searchUserGroupBean = searchUserGroupBean;
	}

	public String getEditMode() {
		return editMode;
	}

	public void setEditMode(String editMode) {
		this.editMode = editMode;
	}

	public String getSortMode() {
		return sortMode;
	}

	public void setSortMode(String sortMode) {
		this.sortMode = sortMode;
	}
	
}
